package com.example.webservice;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button btn;
	private TextView tv;
	private String txt = "";

	Handler handlerSoap = new Handler() {
		public void handleMessage(Message msg) {
			tv.setText(txt);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.btn);
		tv = (TextView) findViewById(R.id.tv);

		btn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					public void run() {
						getSoap();
					}
				}).start();

			}
		});

	}

	public void getSoap() {
		// String nameSpaceAddress = "http://www.w3schools.com/xml/";
		// String methodNameAddress = "CelsiusToFahrenheit";
		// String urlAddress = "http://www.w3schools.com/xml/tempconvert.asmx";
		// String soapActionAddress =
		// "http://www.w3schools.com/xml/CelsiusToFahrenheit";

		String nameSpaceAddress = "http://tempuri.org/";
		String methodNameAddress = "GetDeptInfo";
		String urlAddress = "http://192.168.1.131:808/Project1.dll/soap/ISampleService";
		String soapActionAddress = "http://192.168.1.131:808/Project1.dll/soap/ISampleService";

		SoapObject soapObject = new SoapObject(nameSpaceAddress, methodNameAddress);
		soapObject.addProperty("DeptCode", "01");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.bodyOut = soapObject;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransportSE = new HttpTransportSE(urlAddress);
		try {
			httpTransportSE.call(null, envelope);
		} catch (Exception e) {
			e.printStackTrace();
		}

		SoapObject object = (SoapObject) envelope.bodyIn;
		System.out.println(object);
		txt = object.getProperty(0).toString();
		JSONTokener jsonParser = new JSONTokener(txt);
		try {
			JSONObject json = (JSONObject) jsonParser.nextValue();
			System.out.println(json.getString("data"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		handlerSoap.sendEmptyMessage(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
